
// load icons

// var icon_transparencymap = { coord: [ 1,1, 18,1, 18,20, 1,20 ], type: 'poly' };
var ic = [];
for (var i=0; i<7; i++)
  if (i == 4)
    ic[i] = new google.maps.MarkerImage(
      'icons/i' + (i+1) + '.png',
      new google.maps.Size(24, 24),
      new google.maps.Point(0, 0),
      new google.maps.Point(12, 12)
    );
  else
    ic[i] = new google.maps.MarkerImage(
      'icons/i' + (i+1) + '.png',
      // new google.maps.Size(20, 32),
      new google.maps.Size(16, 16),
      new google.maps.Point(0, 0),
      // new google.maps.Point(0, 31)
      new google.maps.Point(8, 8)
    );

//############

function LDIRMarker(data) {
  var p;
  this.valid = false;
  this.listener = null;

  try {
    p = new google.maps.LatLng(data['lat'], data['lng']);
    this.id = data['id'];
    if (data['name'])
      this.name = data['name'];
    this.desc = data['desc'];
    this.tm = data['tm'];
    if (data['dl']) {
      this.dim = { l: data['dl'], w: data['dw'], h: data['dh'] };
      if (data['bageq'])
        this.dim.bageq = data['bageq'];
      if (data['disp'])
        this.dim.dispersed = true;
    }
    if (data['uid'])
      this.user = { id: data['uid'], un: data['un'], fn: data['fn'] };
    if (data['ph']) {
      this.photos = data['ph'];
      for (var i=0; i<this.photos.length; i++)
        if (this.photos[i].substring(0, 3) == "ws:")
          this.photos[i] = 'http://ldir-mobile.gtstelecom.ro/Images/' + this.photos[i].substring(3);
    }
    this.icnum = data['ic'];

    // google.maps.Marker.apply(this, [ { position: p, icon: ic[this.icnum], flat: true, shape: icon_transparencymap, title: this.desc } ]);
    google.maps.Marker.apply(this, [ { position: p, icon: ic[this.icnum], flat: true, title: this.desc } ]);
    this.valid = true;
  }
  catch(err) {
    if (Const.logging) console.log("ERROR -- LDIRMarker.<init> -- " + err);
  }
}

//############

LDIRMarker.prototype = new google.maps.Marker({position:new google.maps.LatLng(0, 0)});
LDIRMarker.prototype.constructor = LDIRMarker;

//############
var month_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

LDIRMarker.prototype.markerClicked = function(event) {
  function padZero(x) { return ((x < 10) ? '0' : '') + x; }
  try {
    var content = '<div id="content" class="cont"><table>';

    content += '<tr><td>ID:</td><td>' + this.id + '</td></tr>';

    if (this.desc.length > 0)
      content += '<tr><td>Description:</td><td class="b">' + this.desc + '</td></tr>';

    if (this.dim) {
      content += '<tr><td>Size:</td><td>' + this.dim.l.toFixed(1) + ' x ' + this.dim.w.toFixed(1) + ' x ' + this.dim.h.toFixed(1);
      if (this.dim.bageq)
        content += ' &#8211; ' + this.dim.bageq + ' bags';
      if (this.dim.dispersed)
        content += ' (dispersed)';
      content += '</td></tr>';
    }

    var dt = new Date(this.tm*1000);
    content += '<tr><td>Uploaded:</td><td>' + dt.getDate() + ' ' + month_names[dt.getMonth()] + ' ' + dt.getFullYear() + ' ';

    content += padZero(dt.getHours()) + ':' + padZero(dt.getMinutes()) + ':' + padZero(dt.getSeconds()) + '</td></tr>';
    content += '<tr><td>Author:</td><td>' + this.user.un + ' (' + this.user.fn + ')</td></tr>';

    if (this.photos.length > 0) {
      content += '<tr><td>Photos:</td><td>';
      for (var i=0; i<this.photos.length; i++) {
        if (i > 0)
          content += ' &nbsp; ';
        content += '<a href="' + this.photos[i] + '" target="_blank">Photo ' + (i+1) + '</a>';
      }
      content += '</td></tr>';
    }

    content += '<tr><td>Coords:</td><td>( ' + this.position.lat().toFixed(6) + ' , ' + this.position.lng().toFixed(6) + ' )</td></tr>';

    if (this.icnum == 0)
      content += '<tr><td colspan="2">Alocarea se face la punctul de inregistrare.</td></tr>';
      // content += '<tr><td colspan="2"><a href="javascript:alert(\'Alocarea se face la punctul de inregistrare.\')">Aloca morman</a></td></tr>';
      // content += '<tr><td colspan="2"><a href="http://intranet.letsdoitromania.ro/harta/aloca/index.php/alocare/alocam/' + this.id + '">Aloca morman</a></td></tr>';

    content += '</table></div>';
    selectedMarker = this;
    infoWindow.close();
    infoWindow.setContent(content);
    infoWindow.open(map, this);
  }
  catch(err) {
    if (Const.logging) console.log("ERROR -- LDIRMarker.markerClicked -- " + err);
  }
}

//############

LDIRMarker.prototype.toString = function() {
  return "(" + this.id + " (" + this.descr + ") at " + this.getPosition() + ")";
}
